<!DOCTYPE html>
<html>
<head>
<base href="${basePath}">
<meta charset="UTF-8">
<title>会员购买商品</title>
$!{include("import_js_css.html")}
</head>
<body>
	$!{include("header.html")}
	<div class="container" style="width: 1200px;">
		<div class="panel panel-info">
			<!--#var(Member member)-->
			$!{include("memberHeader.html")}
			<div id="div_result_member" style="padding: 15px;">
				<div class="panel">
					$!{include("member_info_tab.html")}
					<div id="div_result_member" class="panel-hxcd-info">
					<form class="form-horizontal" role="form" action="yueyogaAdmin/addGoodsOrder.jhtml" method="post" >
						<input type="hidden" id="memberPrice" value="${memberRemaining}"></input>
						<table class="table table-bordered table-hover table-condensed tb" style="text-align: center;">
							<tr>
								<td style="text-align: center; width:55px;">选择</td>
								<td style="text-align: center; width:150px;">商品编号</td>
								<td style="text-align: center; width:200px;">商品名称</td>
								<td style="text-align: center; width:150px;">商品库存</td>
								<td style="text-align: center; width:150px;">商品价格</td>
								<td style="text-align: center; width:150px;">商品类型</td>
								<td style="text-align: center; width:*;">购买数量</td>
							</tr>
							<tr>
								<td colspan="7" style="margin:0px; padding:0px;" >
									<div style="height:300px; width:100%; overflow:scroll;overflow-x:hidden; text-overflow: ellipsis; white-space: nowrap;">
										<table id="jiaodian" class="table table-condensed" style="text-align: center; width:100%;">
											<tr height="0"></tr>
											<!-- #var(Goods goods, List<Goods> goodsList) -->
											<!--#foreach(goods in goodsList)-->
											<tr>
												<td class="table-edit" align="center" width="54"><input type="checkbox" name="selected_${goods.code}" value="${goods.code}" /></td>
												<td class="table-edit" width="150">${goods.code}</td>
												<td class="table-edit" width="200">${goods.name}</td>
												<td class="table-edit" width="150">${goods.counts}</td>
												<td class="table-edit" width="150">${goods.price}<input type="hidden" class="form-control" value="${goods.price}" id="price_${goods.code}"></td>
												<td class="table-edit" width="150">${goods.goodType.name}</td>
												<td class="table-edit" width="*"><input type="text" style="width:60px" name="counts" id="counts_${goods.code}" required value="1" /></td>
											<tr>
											<!--#end -->
										</table>
									</div>
								</td>
							</tr>
						</table>
						<label style="float:left; margin-top:10px; margin-left:40px;" for="inputName" class="col-sm-1 control-label">商品总价</label>
				        <div style="float:left; margin-top:10px;" class="col-sm-3">
				        	<input type="text" class="form-control"  name="allPrice" id="allPrice" value="${allPrice}" required />
				        </div>
				        <div style="clear:both;"></div>
				        <label style="float:left; margin-top:5px; margin-left:40px;" for="inputNameEn" class="col-sm-1 control-label">优惠金额</label>
				        <div style="float:left; margin-top:5px;" class="col-sm-3" >
				        	<input type="text" class="form-control" name="discountPrice" value="0" id="discountPrice" required />
				        </div>
						<div style="clear:both;"></div>
				        <label style="float:left; margin-top:5px; margin-left:40px;" for="selTkFzrs" class="col-sm-1 control-label">实收金额</label>
				        <div style="float:left; margin-top:5px;" class="col-sm-3">
				        	<input type="text" class="form-control"  name="prices" id="prices" value="0" required />
				        </div>
				        <label for="selTkFzrs" class="col-sm-1 control-label" style="margin-top:-65px; margin-left:140px;" >负责人</label>
						<div class="col-sm-3" style="margin-top:-65px; margin-left:240px;">
							<select id="selTkFzrs" data-live-search="true" class="selectpicker show-tick" data-width="100%" name="seller">
								<option value=""> </option>
								<!-- #for(GroupUserModel respor : responsibleList)-->
								<option value="${respor.id}">${respor.name}</option>
								<!-- #end-->
							</select>
						</div>
						<br /> <br />
						<div class="col-sm-offset-2" style="margin: 2px 32px 0 0px; float: right;">
							<button id="buyGoods" type="button" class="btn btn-default btn-ade-default">购买</button>
						</div>
						<br /><br />
					</form>
					</div>
				</div>
			</div>
		</div>
	</div>
</body>
<script type="text/javascript">
	$(function(){
		var msg = "${msg}";
		if(msg!=null&&msg!=""){
			alert(msg);
		}
		
		//只能选择一个复选框
		$("#jiaodian tr").find("input[type='checkbox']").click(function(){
			$("#jiaodian tr").find(":checkbox:checked").prop("checked",false);
			$(this).prop("checked",true);
		});
		
		$("#prices").bind("input propertychange",function(){
			//获得总价
			var allPrice = $("#allPrice").val();
			//获得实收价格
			var prices = $("#prices").val();
			if(!isFloat(prices)) {
				alert("实收只能为整数或小数！");
				return;
			}else{
				//优惠金额
				var youhui = FloatSub(allPrice,prices);
				$("#discountPrice").val(youhui);
			}
			
		});
		
		$("#jiaodian").find('tr').each(function(){
			$(this).mousemove(function(){
				$(this).css("background-color","#f5f5f5");
			});
			$(this).mouseout(function(){
				$(this).css("background-color","#fff");
			});
		});
		
	});
	
	$("#selTkFzrs").val('${resporsId}');
	var goodsCode="";
	function isInteger(temp) {
		if(parseInt(temp) == temp) return true;
		else return false;
	}
	
	//判断是否为浮点数
	function isFloat(temp) {
		var checkstring = /^[1-9]?[0-9]*[\.]{0,1}[0-9]*$/;
		var res = checkstring.test(temp);
		if(res) return true;
		else return false;
	}
	
	//浮点数乘法运算  
	function FloatMul(arg1,arg2)   
	{   
		var m=0,s1=arg1.toString(),s2=arg2.toString();   
		try {
			m += s1.split(".")[1].length;
		} catch(e) {
			
		}   
		try {
			m+=s2.split(".")[1].length;
		} catch(e) {
			
		}   
		return Number(s1.replace(".","")) * Number(s2.replace(".","")) / Math.pow(10,m);
	}
	
	//浮点数减法运算  
	function FloatSub(arg1,arg2){  
		var r1,r2,m,n;  
		try {
			r1 = arg1.toString().split(".")[1].length;
		} catch(e) {
			r1 = 0;
		}  
		
		try {
			r2 = arg2.toString().split(".")[1].length;
		} catch(e) {
			r2 = 0;
		}  
		m = Math.pow(10,Math.max(r1,r2));  
		//动态控制精度长度  
		n = (r1 >= r2)?r1:r2;  
		return ((arg1 * m - arg2 * m) / m).toFixed(n);  
	}  
	
	
	//会员单选
	$("input[name^='selected_']").click(function() {
		var checked = $(this).prop("checked");
		//如果是选中的，则变为不选中，同时清空商品总价、优惠金额、实收金额
		if(checked) {
			$("input[name^='selected_']").prop("checked", false);
			$(this).prop("checked", true);
			var name = $(this).attr("name");
			var codes = name.split("_");
			//获得商品编号
			var code = codes[1];
			
			//获得价格
			var price = $("input[id='price_" + code + "']").val();
			//获得个数
			var counts = $("input[id='counts_" + code + "']").val();
			if(counts == null || counts.length <= 0 || !isInteger(counts)) {
				if(counts.length <= 0|| !isInteger(counts)){
					$("input[id='counts_" + code + "']").val(1);
					counts = $("input[id='counts_" + code + "']").val();
				}else{
					alert("商品个数不能为空且只能为整数！");
					return;
				}
			}
			
			var allPrice = FloatMul(price, counts);
			
			var discountPrice = $("#discountPrice").val();
			
			if(discountPrice == null || discountPrice.length <= 0) {
				discountPrice = 0;
			} else {
				if(!isFloat(discountPrice)) {
					alert("折扣只能为整数！");
					return;
				} else {
					discountPrice = parseFloat(discountPrice);
				}
			}
			var realPrice = FloatSub(allPrice, discountPrice);
			
			$("#allPrice").val(allPrice);
			$("#discountPrice").val(discountPrice);
			$("#prices").val(realPrice);
			if(goodsCode){
				$("input[id='counts_" + goodsCode + "']").val("1");
			}
			goodsCode=code;
		}else{
			$("#allPrice").val('0');
			$("#discountPrice").val('0');
			$("#prices").val('0');
			if(goodsCode){
				$("input[id='counts_" + goodsCode + "']").val("1");
			}
		}
	});
	
	//商品数量改变
	$("input[id^='counts_']").bind("input propertychange", function() {
		var id = $(this).attr("id");
		var codes = id.split("_");
		var code = codes[1];
		
		var checked = $("input[name='selected_" + code + "']").prop("checked");
		if(checked) {
			var counts = $(this).val();
			if(counts == null || counts.length <= 0 || !isInteger(counts)) {
				if(counts.length <= 0|| !isInteger(counts)){
					$("input[id='counts_" + code + "']").val(1);
					counts = $("input[id='counts_" + code + "']").val();
				}else{
					alert("商品个数不能为空且只能为整数！");
					return;
				}
			}
			
			//获得价格
			var price = $("input[id='price_" + code + "']").val();
			
			//总价
			var allPrice = FloatMul(price, counts);
			
			//折扣
			var discountPrice = $("#discountPrice").val();
			
			if(discountPrice == null || discountPrice.length <= 0) {
				discountPrice = 0;
			} else {
				if(!isFloat(discountPrice)) {
					alert("优惠折扣只能为整数或小数！");
					return;
				}
			}
			//实收金额
			var realPrice = FloatSub(allPrice, discountPrice);
			
			$("#allPrice").val(allPrice);
			$("#discountPrice").val(discountPrice);
			$("#prices").val(realPrice);
		} else {
			alert("请选择该产品！");
		}
	});
	
	//优惠折扣修改
	$("#discountPrice").bind("input propertychange", function() {
		var discountPrice = $(this).val();
		if(discountPrice == null || discountPrice.length <= 0) {
			discountPrice = 0;
		} else {
			if(!isFloat(discountPrice)) {
				alert("优惠折扣只能为整数或小数！");
				return;
			}
		}
		
		//总价
		var allPrice = $("#allPrice").val();
		if(allPrice == null || allPrice.length <= 0) {
			alert("商品总价不能为空，请选择商品！");
			return;
		} else {
			if(!isFloat(allPrice)) {
				alert("商品总价只能为整数或小数！");
				return;
			}
		}
		
		var realPrice = FloatSub(allPrice, discountPrice);
		$("#prices").val(realPrice);
	});
	
	var memberId=${member.memberid};
	//数量文本框数值改变事件
	/* function selectGoods(){
		var checklist = document.getElementsByName("selected");
		var ids="";
		var allPrice=0;
		for(var i=0;i<checklist.length;i++)
		{
			if(checklist[i].checked){
				var id=checklist[i].value;
				ids=ids+id+",";
				var count=$("#counts"+id).val()
				if(count!=null&&count!=""&&!isNaN(parseInt(count))){
					allPrice=allPrice+ parseFloat($("#price"+id).val())*parseInt(count);
				}else{
					if(isNaN(parseInt(count))){
						alert("商品数量应为整数");
						return;
					}
				}
			}
		}
		var allPrice = 0;
		var input = $("input:checkbox:checked");
		if(!input.prop("checked")) {
			alert("请选择商品！");
			return;
		}
		
		var codes = input.val();
		var counts = $("input[id='counts_" + codes + "']").val();
		var price = $("input[id='price_" + codes + "']").val();
		console.log("编号：" + codes + ",数量：" + counts + ",单价：" + price);
		
		if(counts!=null&&counts!=""&&!isNaN(counts)){
			allPrice  = parseFloat(price) * parseFloat(counts);
		}else{
			if(isNaN(counts)){
				alert("商品数量应为整数");
				return;
			}
		}
		$("#allPrice").val(allPrice);
		if(isNaN(parseFloat($("#discountPrice").val()))){
			$("#prices").val(allPrice);
			$("#discountPrice").val("0");
			alert("优惠金额应为数值");
		}else{
			$("#prices").val(allPrice-parseFloat($("#discountPrice").val()));
		}
	} */
	
	//修改商品折扣
	/* function selectGoodsDiscount() {
		var allPrice = $("#allPrice").val();
		var discountPrice = $("#discountPrice").val();
		$("#prices").val(allPrice - discountPrice);
	} */
	
	//购买商品
	$("#buyGoods").click(function() {
		var name = $("input:checkbox:checked").attr("name");
		console.log("选中：" + name);
		if(name == null || name.length <= 0) {
			alert("请选择商品！");
			return;
		}
		
		var codes = name.split("_");
		var code = codes[1];
		
		//获得商品单价
		var price = $("input[id='price_" + code + "']").val();
		//获得商品数量
		var counts = $("input[id='counts_" + code + "']").val(); 	
		if(counts == null || counts.length <= 0) {
			alert("商品数量不能为空！")
			return;
		} else {
			if(!isInteger(counts)) {
				alert("商品数量只能为整数！");
				return;
			}
		}
		
		//总价
		var allPrice = FloatMul(price, counts);
		
		//获得打折
		var discountPrice = $("#discountPrice").val();
		
		if(discountPrice == null || discountPrice.length <= 0) {
			discountPrice = 0;
		} else {
			if(!isFloat(discountPrice)) {
				alert("折扣只能为整数或小数！");
				return;
			} else {
				discountPrice = parseFloat(discountPrice);
			}
		}
		//实收金额
		var realPrice = FloatSub(allPrice, discountPrice);
		//余额是否足够
		var yue = $("#memberPrice").val();
		yue = yue*1;
		if(yue < realPrice){
			alert("余额不足,请及时充值!");
			return;
		}
		//折扣是否大于实收金额
		if(realPrice < 0) {
			alert("折扣不能大于总价！");
			return;
		}
		
		$("#allPrice").val(allPrice);
		$("#discountPrice").val(discountPrice);
		$("#prices").val(realPrice);
		
		var memberId = "${member.memberid}";
		var seller=$("#selTkFzrs").val();
		//ajax提交
		$.post(
			"yueyogaAdmin/addGoodsOrder.jhtml",
			{"id" : memberId, "codes" : code, "counts" : counts, "allPrice" : allPrice, "discountPrice" : discountPrice, "prices" : realPrice,"seller":seller},
			function(msg) {
				if("商品购买失败!"==msg){
					alert(msg);
					return;
				}else{
						window.location.href="yueyogaAdmin/toMemberGoods.jhtml?id="+memberId;
				}
			}
		);
	});
	/* function buyGoods(){
		var checklist = document.getElementsByName("selected");
		var codes=$("input:checkbox:checked").val();
		var counts = $("input[id='counts_" + codes + "']").val();
		for(var i=0;i<checklist.length;i++)
		{
			if(checklist[i].checked){
				var id=checklist[i].value;
				codes=codes+id+",";
				var count=$("#counts"+id).val()
				if(count&&!isNaN(parseInt(count))){
					counts=counts+ count+",";
				}else{
					alert("请正确填写商品数量");
					return;
				}
			}
		}
		if(counts&&!isNaN(parseInt(counts))){
			
		}else{
			alert("请正确填写商品数量");
			return;
		}
		var allPrice=$("#allPrice").val();
		var discountPrice=$("#discountPrice").val();
		var prices=$("#prices").val();
		var url = "yueyogaAdmin/addGoodsOrder.jhtml";
		$.post(url,{
			id:memberId,
			codes:codes,
			counts:counts,
			allPrice:allPrice,
			discountPrice:discountPrice,
			prices:prices
		},function(msg){
			if("商品购买失败!"==msg){
				alert(msg);
				return;
			}else{
				var d = alert(msg);
				setTimeout(function(){
					d.close().remove();
					window.location.href="yueyogaAdmin/toMemberGoods.jhtml?id="+memberId;
				}, 1000);
			}
		});
	} */
</script>
</html>
